package lk.mrt.cse.wihidum.router.processor;

import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.AxisFault;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.log4j.Logger;

import javax.xml.namespace.QName;

public class PublicationProcessor
{
    private static final Logger logger = Logger.getLogger (PublicationProcessor.class);

    public String publish (String brokerAddress, String adminAddress, String publisherAddress, String topic, String publicationText)
   {
       try
       {
           ServiceClient serviceClient = new ServiceClient ();
           Options options = new Options ();
           options.setTo (new EndpointReference(publisherAddress));
           options.setAction ("publish");
           serviceClient.setOptions (options);

           OMFactory factory = OMAbstractFactory.getOMFactory ();

           OMElement messageElement = factory.createOMElement ("PublicationRequest", null);

           OMElement publicationHeader = factory.createOMElement ("Header", null);
           OMElement actionElement = factory.createOMElement ("Action", null);
           actionElement.setText ("publish");
           publicationHeader.addChild (actionElement);
           messageElement.addChild (publicationHeader);

           OMElement brokerAddressElement = factory.createOMElement ("brokerAddress", null);
           brokerAddressElement.setText (brokerAddress);
           messageElement.addChild (brokerAddressElement);

            OMElement adminAddressElement = factory.createOMElement("adminAddress", null);
            adminAddressElement.setText(adminAddress);
            messageElement.addChild(adminAddressElement);

           OMElement publisherAddressElement = factory.createOMElement ("publisherAddress", null);
           publisherAddressElement.setText (publisherAddress);
           messageElement.addChild (publisherAddressElement);

           OMElement topicElement = factory.createOMElement ("topic", null);
           topicElement.setText (topic);
           messageElement.addChild (topicElement);

           OMElement publicationTextElement = factory.createOMElement ("publicationText", null);
           publicationTextElement.setText (publicationText);
           messageElement.addChild (publicationTextElement);

           OMElement responseElement = serviceClient.sendReceive (messageElement);

           OMElement referenceParameters = responseElement.getFirstChildWithName (new QName("ReferenceParameters"));
           OMElement responseTextElement = referenceParameters.getFirstChildWithName (new QName ("Response"));

           logger.info (responseTextElement.getText ());
           return responseTextElement.getText();
       }
       catch (NullPointerException ex)
       {
           logger.info ("Error in processing publication response");
           return "Error in processing publication response";
       }
       catch (AxisFault ex)
       {
           logger.info ("Error in sending publication request");
           return "Error in sending publication request";
       }
   }
}
